package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.zgl.youshuda.core.service.procservice.util.callservice.ServerInfoUtil;
import com.zgl.youshuda.core.service.procservice.util.callservice.domain.MksServerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysInfoMapper;
import com.ruoyi.system.domain.SysInfo;
import com.ruoyi.system.service.ISysInfoService;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统信息Service业务层处理
 *
 * @author ZGL
 * @date 2025-03-10
 */
@Service
public class SysInfoServiceImpl implements ISysInfoService
{
    @Autowired
    private SysInfoMapper sysInfoMapper;

    /**
     * 当前服务器信息
     */
    @Autowired
    private ServerInfoUtil serverInfoUtil;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询系统信息
     *
     * @param id 系统信息主键
     * @return 系统信息
     */
    @Override
    public SysInfo selectSysInfoById(Integer id)
    {
        return sysInfoMapper.selectSysInfoById(id);
    }

    public SysInfo selectSysInfo(){
        List<SysInfo> sysInfos = sysInfoMapper.selectSysInfoList(null);
        if(sysInfos!=null && sysInfos.size()>0){
            return sysInfos.get(0);
        }
        return null;
    }

    /**
     * 查询系统信息列表
     *
     * @param sysInfo 系统信息
     * @return 系统信息
     */
    @Override
    public List<SysInfo> selectSysInfoList(SysInfo sysInfo)
    {
        return sysInfoMapper.selectSysInfoList(sysInfo);
    }

    /**
     * 新增系统信息
     *
     * @param sysInfo 系统信息
     * @return 结果
     */
    @Override
    public int insertSysInfo(SysInfo sysInfo)
    {
        sysInfo.setCreateTime(DateUtils.getNowDate());
        return sysInfoMapper.insertSysInfo(sysInfo);
    }

    /**
     * 修改系统信息
     *
     * @param sysInfo 系统信息
     * @return 结果
     */
    @Override
    public int updateSysInfo(SysInfo sysInfo)
    {
        sysInfo.setUpdateTime(DateUtils.getNowDate());
        return sysInfoMapper.updateSysInfo(sysInfo);
    }

    /**
     * 批量删除系统信息
     *
     * @param ids 需要删除的系统信息主键
     * @return 结果
     */
    @Override
    public int deleteSysInfoByIds(Integer[] ids)
    {
        return sysInfoMapper.deleteSysInfoByIds(ids);
    }

    /**
     * 删除系统信息信息
     *
     * @param id 系统信息主键
     * @return 结果
     */
    @Override
    public int deleteSysInfoById(Integer id)
    {
        return sysInfoMapper.deleteSysInfoById(id);
    }

    /**
     * 获取优树搭服务地址
     * @param request
     * @return
     */
    public String getYsdBaseUrl(HttpServletRequest request){
        SysInfo sysInfos = selectSysInfo();
        if(sysInfos!=null && StringUtils.isNotEmptyIncludeNull(sysInfos.getYsdBaseUrl())){
            return sysInfos.getYsdBaseUrl();
        }else{
            /**
             * 获取当前服务器信息
             */
            MksServerInfo mksServerInfo = serverInfoUtil.getMksServerInfo();
            String reqType="http";
            String reqPort=":" + mksServerInfo.getPort();
            if(request.isSecure() || "https".equalsIgnoreCase(request.getHeader("X-Forwarded-Proto"))){
                reqType="https";
                reqPort=":443";
            }
            reqType+="://"+mksServerInfo.getIp()+ reqPort + mksServerInfo.getContextPath();

            return reqType;
        }
    }

    public boolean saveSysInfo(SysInfo sysInfo){
        LoginUser loginUser = SecurityUtils.getLoginUser();

        SysInfo oldSysInfos = selectSysInfo();

        int dealCount=0;

        if (oldSysInfos == null)
        {
            //新增
            sysInfo.setCreateBy(loginUser.getUsername());
            sysInfo.setCreateTime(new Date());
            dealCount = sysInfoMapper.insertSysInfo(sysInfo);
        }else{
            //修改
            sysInfo.setId(oldSysInfos.getId());
            sysInfo.setUpdateBy(loginUser.getUsername());
            sysInfo.setUpdateTime(new Date());
            //logo地址
            dealCount = sysInfoMapper.updateSysInfo(sysInfo);
        }
        redisCache.setCacheObject(CacheConstants.SYS_CONFIG_KEY+"expire_time", sysInfo.getExpireTime());
        return dealCount>0;
    }

    /**
     * 保存LOGO
     * @param logo
     * @return
     */
    public boolean saveLogo(String logo){

        LoginUser loginUser = SecurityUtils.getLoginUser();

        List<SysInfo> sysInfos = sysInfoMapper.selectSysInfoList(null);

        int dealCount=0;

        if (sysInfos == null || sysInfos.size()<=0)
        {
            //新增
            SysInfo config= new SysInfo();
            config.setLogo(logo);
            config.setCreateBy(loginUser.getUsername());
            config.setCreateTime(new Date());
            dealCount = sysInfoMapper.insertSysInfo(config);
        }else{
            //修改
            SysInfo config = sysInfos.get(0);
            config.setUpdateBy(loginUser.getUsername());
            config.setUpdateTime(new Date());
            //logo地址
            config.setLogo(logo);
            dealCount = sysInfoMapper.updateSysInfo(config);
        }
        return dealCount>0;
    }

    /**
     * 保存登录页背景图片
     * @param loginBgUrl
     * @param location
     * @return
     */
    public boolean saveloginBg(String loginBgUrl,String location){

        LoginUser loginUser = SecurityUtils.getLoginUser();

        List<SysInfo> sysInfos = sysInfoMapper.selectSysInfoList(null);

        int dealCount=0;

        if (sysInfos == null || sysInfos.size()<=0)
        {
            //新增
            SysInfo config= new SysInfo();
            config.setLoginBg(loginBgUrl);
            config.setLoginBoxLocation(location);
            config.setCreateBy(loginUser.getUsername());
            config.setCreateTime(new Date());
            dealCount = sysInfoMapper.insertSysInfo(config);
        }else{
            //修改
            SysInfo config = sysInfos.get(0);
            config.setUpdateBy(loginUser.getUsername());
            config.setUpdateTime(new Date());
            config.setLoginBg(loginBgUrl);
            config.setLoginBoxLocation(location);
            dealCount = sysInfoMapper.updateSysInfo(config);
        }
        return dealCount>0;
    }
}
